﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HtmlAgilityPack;
using NewsMine.DomainObjects;
using NewsMine.Storage;
using NewsDiscoveryEngine;
using WebApp1.Models;

namespace WebApp1.AdminSection
{
    public partial class BulkWebSiteInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SecurityCheckManager.IsAdminPortalEabled() == false)
            {
                Response.Write("You should be the admin....");
                return;
            }


            if (this.IsPostBack == false)
                return;

            if (this.Request.Files == null || this.Request.Files.Count == 0)
            {
                return;
            }

            IKeyValueStore webSiteInfoStore = Defaults.WebSiteInfoMasterStore;

            webSiteInfoStore.Open();

            if (this.Request.Files.Count > 0)
            {
                var file = this.Request.Files["file"];

                if (file == null || file.ContentLength == 0)
                    return;

                HtmlWeb web = new HtmlAgilityPack.HtmlWeb();

                HtmlDocument doc = new HtmlDocument();
                doc.Load(file.InputStream);

                var rows = doc.DocumentNode.SelectNodes("//tr");

                string homeUrl = string.Empty;
                string name = string.Empty;
                string launguage = string.Empty;

                foreach (var row in rows)
                {
                    if (row.ChildNodes.Count < 3)
                        continue;
                    var cells = (from cell in row.ChildNodes
                                 where cell.Name != null && cell.Name.ToLower() == "td"
                                 select cell).ToList();


                    if (cells.Count < 3)
                        continue;

                    homeUrl = HttpUtility.HtmlDecode(cells[0].InnerText);

                    if (!homeUrl.Trim().StartsWith("http"))  //means this row is header of the table. so, lets skip this.
                        continue;

                    //this is bug in html generation... 
                    if (homeUrl.Contains("=\r\n"))
                        homeUrl = homeUrl.Replace("=\r\n", string.Empty);

                    name = HttpUtility.HtmlDecode(cells[1].InnerText);

                    launguage = HttpUtility.HtmlDecode(cells[2].InnerText);

                    Uri baseUri = null;
                    try
                    {
                        baseUri = new Uri(homeUrl);
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<br/>Error while parsing row <br/> Url " + homeUrl + "<br/> " + ex.Message);
                        continue;
                    }

                    if (baseUri == null)
                        continue;

                    WebSiteInfo storedWebSiteInfo = (WebSiteInfo)webSiteInfoStore.Get(baseUri.DnsSafeHost);

                    if (storedWebSiteInfo != null) //if the website is there already, we will just skip this.
                        continue;

                    WebSiteInfo webSiteInfo = new WebSiteInfo { Domain = baseUri.DnsSafeHost, HomePageUrl = homeUrl, Name = name, Language = launguage };

                    webSiteInfoStore.Replace(webSiteInfo.Domain, webSiteInfo);

                    Response.Write(webSiteInfo.Name + " is included in the datastore. <br/>");
                }

                webSiteInfoStore.Close();

            }

        }
    }
}